// pages/UserStatistics/Userindex/UserIndex.js
const controllers = require('../../../../../controllers/controllerIndex.js').controller;
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    time: false,
    pay: false,
    activeIndex: 0,
    tabList: [{
        name: "全部",
        // type: 0,
        vip: null,
      },
      {
        name: "普通用户",
        // type: 1,
        vip: 0,
      },
      {
        name: "会员",
        vip: 2,
      },
      {
        name: "VIP会员",
        vip: 1,
      },
      {
        name: "股东",
        vip: 3,
      },
      {
        name: "代理商",
        vip: 4,
      }
    ],
    index: 1,
    inputText: '',
    icon1: "https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20210414/831918901884354560.png",
    icon2: "https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20210414/831919079068532736.png",
    store: "",
    pageNumber: 1,
    userList:[],
    selectList:[],
    lastPage: false
  },

  //获取输入数据
  inputSearch(e) {
    this.setData({
      inputText: e.detail.value,
    })
  },
  // 搜索
  searchUser(){
    this.setData({
      userList:[],
      pageNumber:1
    })
    this.getUserList()
  },

  getUserList() {
    let params = {
      sessionId: wx.getStorageSync('token'),
      shopId: wx.getStorageSync('shopId'),
      pageNumber:this.data.pageNumber,
      time:"ASC"
    }
    if (this.data.index == 0) {
      params.nick = this.data.inputText
    } else {
      params.mobile = this.data.inputText
    }

    let item = this.data.tabList[this.data.activeIndex];
    if (item.vip !=null ) params.vip = item.vip;

    controllers.accountListPage(
      params
    ).then(res => {
      let userList=this.data.userList.concat(res.data.list)
      this.data.userList.forEach(item => item.select = false)
      this.setData({
        userList,
        lastPage: res.data.lastPage
      })
    })
  },

  getMore() {
    if (this.data.lastPage) return
      const pageNumber = this.data.pageNumber + 1
      this.setData({
        pageNumber
      })
      this.getUserList()
  },
  // 切换用户类型
  changeTab(e) {
    const index = e.currentTarget.dataset.index
    this.setData({
      activeIndex: index,
      pageNumber:1,
      userList:[]
    });
    this.getUserList()
  },
  // 选择用户
  selectUser(e) {
    const index = e.currentTarget.dataset.index
    let userList = this.data.userList;
    userList[index].select = !userList[index].select;
    const selectList=userList.filter(item=>{
      return item.select
    })
    this.setData({
      userList,
      selectList
    });
  },
  cancel(){
    wx.navigateBack()
  },
  confirm(){
    const selectList=JSON.stringify(this.data.selectList)
    wx.setStorageSync('selectList', selectList)
    wx.navigateBack()
  },

  onLoad: function () {
    this.getUserList();
  },
  onReachBottom: function () {
    this.getMore()
  },

})